import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginAuthentication extends HttpServlet{

 private ServletConfig config;
  
  public void init(ServletConfig config)
    throws ServletException{
     this.config=config;
     }
  public void doPost(HttpServletRequest request, HttpServletResponse response) 
              throws ServletException,IOException{
      
    PrintWriter out = response.getWriter();
    String connectionURL = "jdbc:mysql://localhost/jeu_pendu";
    Connection connection=null;
    ResultSet rs;
    String userName=new String("");
    String passwrd=new String("");
    response.setContentType("text/html");
    
    //1-  On recherche tous les users dans la base
   try {
       //Load the database driver
      Class.forName("com.mysql.jdbc.Driver");
      // Get a Connection to the database
      connection = DriverManager.getConnection(connectionURL, "elvis", "elvis"); 
      //Add the data into the database
      String sql = "select Nom,Prenom from personne";
      Statement s = connection.createStatement();
      s.executeQuery (sql);
      rs = s.getResultSet();
      while (rs.next ()){
       userName=rs.getString("Nom");
       passwrd=rs.getString("Prenom");
      }
       rs.close ();
       s.close ();
     }catch(Exception e){
      System.out.println("Exception is ;"+e);
	 
    	  // TODO Auto-generated catch block
		 // e.printStackTrace();
       }
   
   //2- et  on confronte aux valeurs saisis:intrus ou non
      if(userName.equals(request.getParameter("user")) 
             && passwrd.equals(request.getParameter("pass"))){
        out.println("Bienvenue "+userName);
        out.println("<a href='jsp/pendu.jsp'><br>Play</a>");
      }
      else{
        out.println("Please enter correct username and password");
        out.println("<a href='AuthenticLogin.jsp'><br>Login again</a>");
      }
  }
}  